package com.xyy.ec.qualification.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.Properties;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LoadProperties {
    private static final Logger logger = LoggerFactory.getLogger(LoadProperties.class);
    private static final Lock lock = new ReentrantLock();
    private static LoadProperties loadProperties;
    private Properties properties = new Properties();

    public static LoadProperties get() {
        if(loadProperties != null) {
            return loadProperties;
        }

        lock.lock();

        try {
            if(loadProperties == null) {
                loadProperties = new LoadProperties();
            }
        }
        finally {
            lock.unlock();
        }

        return loadProperties;
    }

    public String getProperty(String key) {
        if(properties == null){
            get();
        }
        if (null == properties){
            return null;
        }
        return properties.getProperty(key);
    }

    public void setProperty(String key,String value) {
        properties.setProperty(key,value);
    }
    private LoadProperties() {
        try {
            String projectPath = System.getProperty(Constant.DIR);
            //String projectPath = "/Users/jiangxing/IdeaProjects/ybm/pop-datasync-client";
            logger.info("________________projectPath={}", projectPath);
            //读取配置文件

            BufferedReader bufferedReader = new BufferedReader(new FileReader(projectPath + File.separator + "erp_preference" + File.separator + "crawler-plugin-config.properties"));

            properties = new Properties();
            properties.load(bufferedReader);
             bufferedReader.close();
        }
        catch(Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }

    public void writeProperties(){
        try {
            String projectPath = System.getProperty(Constant.DIR);
            String filePath = projectPath + File.separator + "erp_preference" + File.separator + "crawler-plugin-config.properties";
            File yourFile = new File(filePath);
            createIfNotExist(filePath);
            FileOutputStream outputStream = new FileOutputStream(yourFile, false);
            properties.store(outputStream, null);
            outputStream.close();
        } catch (Exception e) {
            logger.info("", e);
        }
    }


    private static void createIfNotExist(String path){
        File yourFile = new File(path);
        try{
            if (!yourFile.exists()){
                if (!yourFile.getParentFile().exists()){
                    yourFile.getParentFile().mkdirs();
                }
                yourFile.createNewFile();
            }
        }catch (Exception e){
            logger.error("writeProperties_path={}", path, e);
        }
    }
}
